package com.whzjkj.gateway;

import com.google.common.util.concurrent.ThreadFactoryBuilder;
import com.whzjkj.gateway.server.tcp.listener.TCPServerListener;
import com.whzjkj.gateway.server.udp.listener.UDPServerListener;
import org.springframework.boot.CommandLineRunner;
import org.springframework.boot.SpringApplication;
import org.springframework.boot.autoconfigure.SpringBootApplication;

import javax.annotation.Resource;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;
import java.util.concurrent.ThreadFactory;

@SpringBootApplication
public class TerminalGatewayServerApplication implements CommandLineRunner {

    @Resource
    private TCPServerListener tcpServerListener;
    @Resource
    private UDPServerListener udpServerListener;

    public static void main(String[] args) {
        SpringApplication.run(TerminalGatewayServerApplication.class, args);
    }

    @Override
    public void run(String... args) {

        ThreadFactory namedThreadFactory = new ThreadFactoryBuilder().setNameFormat("netty-thread-%d").build();
        ExecutorService pool = Executors.newFixedThreadPool(2, namedThreadFactory);
        pool.execute(()-> new TCPServerThread().run());
        pool.execute(()-> new UDPServerThread().run());

    }

    /**
     * netty服务启动线程 . <br>
     *
     * @author hkb
     */
    private class TCPServerThread implements Runnable {

        @Override
        public void run() {
            tcpServerListener.start();
        }
    }

    /**
     * netty服务启动线程 . <br>
     *
     * @author hkb
     */
    private class UDPServerThread implements Runnable {

        @Override
        public void run() {
            udpServerListener.start();
        }
    }
}
